/**
 * Created by dh.huang on 2016/8/26.
 */

angular.module('inspinia')
  .controller('userRegisterCtrl', function($scope, $http, $modal, $rootScope,values) {
    var users = values.users;
  $scope.sendMobile = function () {
    if ($scope.username == '') {
      toaster.pop({
        type: 'error',
        title: '请输入用户名',
        showCloseButton: true,
        timeout: 2200
      });
    }
    else if (!(/1\d{10}$/.test($scope.username))) {
      toaster.pop({
        type: 'error',
        title: '请输入正确的手机号码',
        showCloseButton: true,
        timeout: 2200
      });
    }
    else {
/*      $http.get($rootScope.servers + '/service/user/checkcode?username=' + $scope.username)
        .success(function (data) {
          var result = data.result;
          var num_t2;
          if (result == 0) {
            var num2 = 60;
            num_t2 = setInterval(function () {
              num2--;
              if (num2 > 0) {
                $(".code_btn2").html(num2 + "秒后重新发送");
                $(".code_btn2").attr("disabled", "disabled");
              } else {
                clearTimeout(num_t2);
                $(".code_btn2").html("发送验证码");
                $(".code_btn2").removeAttr("disabled");
              }
            }, 1000);
          }
          else if (result == -1) {
            var num2 = 60;
            num_t2 = setInterval(function () {
              num2--;
              if (num2 > 0) {
                $(".code_btn2").html(num2 + "秒后重新发送");
                $(".code_btn2").attr("disabled", "disabled");
              } else {
                clearTimeout(num_t2);
                $(".code_btn2").html("发送验证码");
                $(".code_btn2").removeAttr("disabled");
              }
            }, 1000);

            toaster.pop({
              type: 'error',
              title: '发送失败',
              showCloseButton: true,
              timeout: 2000
            });
          }
          else {
            toaster.pop({
              type: 'error',
              title: '服务器连接异常',
              showCloseButton: true,
              timeout: 2000
            });
          }
        })*/

    }
  }
  $scope.resetPassword = function () {
    $http.post($rootScope.servers + '/service/user/findpassword', {
      'username': $scope.username,
      'checkcode': $scope.checkcode
    })
      .success(function (data) {
        var result = data.result;
        if (result == 0) {
          toaster.pop({
            type: 'success',
            title: '重置成功',
            body: '密码重置成功，初始密码为:123456。请点击“返回”，然后登录。',
            showCloseButton: true,
            timeout: 1200
          });
        }
        else if (result == -1) {
          toaster.pop({
            type: 'error',
            title: '密码重置失败',
            showCloseButton: true,
            timeout: 600
          });
        }
        else if (result == 1) {
          toaster.pop({
            type: 'error',
            title: '密码重置失败',
            body: '用户不存在',
            showCloseButton: true,
            timeout: 600
          });
        }
        else {
          toaster.pop({
            type: 'error',
            title: '密码重置失败',
            body: '服务器异常',
            showCloseButton: true,
            timeout: 600
          });
        }
      })
  }

  $scope.open = function () {
    var modalInstance = $modal.open({
      templateUrl: 'app/main/Viewer/userServicePolicy.html',
      controller: ModalInstanceCtrl,
      windowClass: "animated fadeIn"
    });
  };

  $scope.usertype = 2;
  $scope.changeUserType = function (type) {
    $scope.usertype = type;
    if (type == 1) {
      $(".doctor").removeClass('btn-success').addClass('btn-white');
      $(".person").removeClass('btn-white').addClass('btn-success');
    }
    else {
      $(".doctor").removeClass('btn-white').addClass('btn-success');
      $(".person").removeClass('btn-success').addClass('btn-white');
    }
  }

  $scope.register = function () {
    if ($scope.password != $scope.password2) {
      toaster.pop({
        type: 'error',
        title: '登录失败',
        body: '两次密码输入不同',
        showCloseButton: true,
        timeout: 1600
      });
    }

    else {

      var registerData = {
        usertype: $scope.usertype,
        username: $scope.username,
        password: $scope.password,
        email: $scope.email,
        checkcode: $scope.checkcode
      };

      $http.post($rootScope.servers + '/service/user/register', registerData)
        .success(function (data) {
          var result = data.result;
          if (result == 0) {
            toaster.pop({
              type: 'success',
              title: '注册成功',
              body: '恭喜你注册成功',
              showCloseButton: true,
              timeout: 600
            })
            window.location.href = '/#/login';
          }
          else if (result == 1) {
            toaster.pop({
              type: 'error',
              title: '注册失败',
              body: '用户名已经注册',
              showCloseButton: true,
              timeout: 600
            })
          }
          else if (result == 2) {
            toaster.pop({
              type: 'error',
              title: '注册失败',
              body: '短信校验码错误',
              showCloseButton: true,
              timeout: 600
            })
          }
          else if (result == 3) {
            toaster.pop({
              type: 'error',
              title: '注册失败',
              body: '短信校验码过期',
              showCloseButton: true,
              timeout: 600
            })
          }
          else if (result == -1) {
            toaster.pop({
              type: 'error',
              title: '注册失败',
              // body: '短信校验码错误',
              showCloseButton: true,
              timeout: 600
            })
          }
          else {
            toaster.pop({
              type: 'error',
              title: '注册失败',
              body: '服务器异常',
              showCloseButton: true,
              timeout: 600
            })
          }
        })
    }
  }
});
